﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using SeguradoraIA.Framework.Log;

namespace SeguradoraIA.Framework.Configuration
{
    public class Configurator
    {
        private readonly FrameworkConfigurationSection frameworkConfigurationSection;
        private static Configurator instance;

        private Configurator()
        {
            frameworkConfigurationSection =
                ConfigurationManager.GetSection("frameworkConfiguration") as FrameworkConfigurationSection;

            if (frameworkConfigurationSection == null)
                throw new Exception("Configurator - Nao foi possivel configurar o framework, verifique a configuracao do Web.config do servico!");
        }

        public static Configurator GetInstance()
        {
            if (instance == null)
            {
                instance = new Configurator();
            }

            return instance;
        }


        public string GetDaoFactoryConfiguration(string name)
        {
            string ret = string.Empty;

            for (int count = 0; count < frameworkConfigurationSection.DaoFactories.Count; count++)
            {
                var factory = frameworkConfigurationSection.DaoFactories.Get(count);
                if (factory.Interface == name)
                {
                    ret = factory.AssemblyName + "|" + factory.TypeName;
                    break;
                }
            }

            return ret;
        }

        public string GetResourceFullTypeName()
        {
            return frameworkConfigurationSection.ResourceFile.FullTypeName;
        }

        public void GetLoggers(List<ILogger> loggers)
        {
            for (int count = 0; count < frameworkConfigurationSection.Loggers.Count; count++)
            {
                var loggerConfiguration = frameworkConfigurationSection.Loggers.Get(count);

                ILogger loggerInstance = (ILogger)Assembly.Load(loggerConfiguration.AssemblyName).CreateInstance(
                                                      loggerConfiguration.TypeName, true, BindingFlags.Instance | BindingFlags.Public,
                                                      null, new object[] { loggerConfiguration.Arguments }, System.Globalization.CultureInfo.CurrentCulture, null);

                if (!string.IsNullOrEmpty(loggerConfiguration.Listen))
                {
                    loggerInstance.ListenLogMessage = loggerConfiguration.Listen.Contains("logMessage");
                    loggerInstance.ListenLogReturn = loggerConfiguration.Listen.Contains("logReturn");
                    loggerInstance.ListenLogSQL = loggerConfiguration.Listen.Contains("logSQL");
                    loggerInstance.ListenLogEntry = loggerConfiguration.Listen.Contains("logEntry");
                    loggerInstance.ListenLogException = loggerConfiguration.Listen.Contains("logException");
                }

                loggers.Add(loggerInstance);
            }

        }

        public void GetLoggerPublishingStatus(ref bool logMessage, ref bool logReturn, ref bool logSQL, ref bool logEntry, ref bool logException)
        {
            string publishRule = frameworkConfigurationSection.Loggers.Publish;
            if (!string.IsNullOrEmpty(publishRule))
            {
                logMessage = publishRule.Contains("logMessage");
                logReturn = publishRule.Contains("logReturn");
                logSQL = publishRule.Contains("logSQL");
                logEntry = publishRule.Contains("logEntry");
                logException = publishRule.Contains("logException");
            }

        }
    }
}